﻿using DevExpress.XtraReports.UI;
using System;
using System.Data;
using System.Drawing;

namespace ConfExpert.Forms.Rapor.Aker
{
    public partial class UrunEtiketi : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        int EmptyLabelCount;
        public bool Stop = false;
        public UrunEtiketi()
        {
            InitializeComponent();
        }

        public UrunEtiketi(DataSet ds1, int emptyLabelCount)
            : this()
        {
            EmptyLabelCount = emptyLabelCount;

            //ds = Session.DataManager.ExecuteDs("GetUrunEtiketi", productIds);
            ds = ds1;
            
            for (int i = 0; i < EmptyLabelCount; i++)
            {
                DataRow r= ds.Tables["Table"].NewRow();
                ds.Tables["Table"].Rows.InsertAt(r, 0);
            }


            this.DataMember = "Table";
            this.DataSource = ds;

            tcArticle.DataBindings.Add("Text", null, "Table.Article");
            tcKompozisyon.DataBindings.Add("Text", null, "Table.Composition");
            tcWidth.DataBindings.Add("Text", null, "Table.Width");
            tcWeight.DataBindings.Add("Text", null, "Table.Weight");
            xrBarCode1.DataBindings.Add("Text", null, "Table.ProductId");
            tcPrice.DataBindings.Add("Text", null, "Table.EUR");
            tcDesign.DataBindings.Add("Text", null, "Table.Design");

            //if (!IsCons)
            //{
            //    trKonstruksiyon.Dispose();
            //}

            //if (!IsBarkod)
            //{
            //    trBarkod.Dispose();
            //}
            
        }       
        
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            EmptyLabelCount--;
        }

        
        private void ReSize(object sender)
        {

            //Resize ı düzgün yapan tek fonksiyon !!!
            Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
            gr.PageUnit = GraphicsUnit.Millimeter;
            int factor = 10;

            XRLabel label = (XRLabel)sender;

            float newSize = 12f;
            Font defaultFont = new System.Drawing.Font("Calibri", newSize);

            float stringWidth = gr.MeasureString(label.Text, defaultFont).Width;
            float labelWidth = label.WidthF;


            if (labelWidth < stringWidth * factor)
                newSize = defaultFont.Size * label.WidthF / stringWidth / factor;


            label.Font = new Font(defaultFont.FontFamily, newSize);
            gr.Dispose();

            
        }    
              
        
        private void tcKompozisyon_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }        

        private void xrBarCode1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrTable2_BeforePrint_1(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tbKumas_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void xrPictureBox1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            if (EmptyLabelCount > -1)
            {
                e.Cancel = true;
            }
        }

        private void tcArticle_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcDesign_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWeight_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcWidth_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

        private void tcPrice_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            ReSize(sender);
        }

    }
}
